/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.template;

import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ow2.bonita.light.LightProcessDefinition;

/**
 *
 * @author marat
 */
public class ProcessButton extends CssLayout {

    private static final long serialVersionUID = 1L;
    private LightProcessDefinition process;
    private static final String STYLENAME = "message-button";

    public ProcessButton(LightProcessDefinition process) {
        try {
            this.process = process;

            setWidth("100%");
            setStyleName(STYLENAME);


            String processName = process.getLabel() != null ? process.getLabel() : process.getName();
            String processFullName = processName + "  ver." + process.getVersion();
            String processDescription = process.getDescription();

            Label processLabel = new Label();
            processLabel.setHeight("1.5em");
            processLabel.setWidth("-1px");
            processLabel.setStyleName(STYLENAME + "-process");
            if (processFullName.length() > 35) {
                processLabel.setValue(processFullName.substring(0, 35) + "...");
            } else {
                processLabel.setValue(processFullName);
            }
            addComponent(processLabel);

            if (process.getDescription() != null) {
                Label categoryLabel = new Label();
                if (processDescription.length() > 35) {
                    categoryLabel.setValue(processDescription.substring(0, 35).replaceAll("\\<.*?\\>", "") + "...");
                } else {
                    categoryLabel.setValue(processDescription.replaceAll("\\<.*?\\>", ""));
                }
                categoryLabel.setWidth("-1px");
                categoryLabel.setStyleName(STYLENAME + "-title");
                addComponent(categoryLabel);
            }
        } catch (Exception ex) {
            Logger.getLogger(ProcessButton.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public LightProcessDefinition getProcess() {
        return process;
    }
}
